using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


namespace OrbitOne.EmailProtector
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:EmailProtectorControl runat=server></{0}:EmailProtectorControl>")]
    public class EmailProtectorControl : System.Web.UI.WebControls.WebControl, INamingContainer
    {
        #region Members
        private IEncryptor encryptor = EncryptorFactory.GetEncryptor();
        private string _emailAddress = string.Empty;
        private string _key = "Iate1milrve%ster";
        private HtmlGenericControl _divResultContainer = new HtmlGenericControl();
        private bool _createLink = true;
		private string _linkText = string.Empty;
		#endregion Members


        #region Properties

        [Bindable(true), Category("Data"),  Description("This value will be used as encryption key when needed."), Localizable(true)]
        public string Key
        {
            get
            {
               
                return (this._key);
            }

            set
            {
                _key = value;
            }
        }

        [Bindable(true), Category("Data"), Description("The email address to be encrypted."), DefaultValue(""), Localizable(true)]
        public string EmailAddress
        {
            get
            {
                return _emailAddress;
            }

            set
            {
                _emailAddress = value;
            }
        }



		[Bindable(true), Category("Data"), Description("Indicates whether a link will be created for the email address. Default value is true."),DefaultValue(true), Localizable(true)]
		public bool CreateLink
		{
			get
			{
				return _createLink;
			}

			set
			{
				_createLink = value;
			}
		}


		[Bindable(true), Category("Data"), DefaultValue("The text of the link."), Localizable(true)]
		public string LinkText
		{
			get
			{
				return _linkText;
			}

			set
			{
                _linkText = value;
			}
		}






        #endregion Properties






        #region Inherited Methods

        protected override void RenderContents(HtmlTextWriter output)
        {
#if NET_2_0 
            if (!DesignMode)
            {
#endif
                String javascript = GetDecryptCallingJavascript();
                _divResultContainer.InnerHtml = javascript;
                _divResultContainer.RenderControl(output);
#if NET_2_0
		}
#endif
        }

        private String GetDecryptCallingJavascript()
        {
            StringBuilder javascript = new StringBuilder();
            javascript.Append("\n<script type=\"text/javascript\">\n");
            javascript.Append("<!--\n");

            if (CreateLink)
            {
                javascript.Append(@"document.write(""<a href=mailto:"");");
                AppendDecryptCallScript(javascript);
                javascript.Append(@"document.write("">"");");
                if (LinkText != string.Empty)
                {
                    javascript.Append(@"document.write(""");
                    javascript.Append(LinkText);
                    javascript.Append(@""");");
                }
                else
                {
                    AppendDecryptCallScript(javascript);
                }

                javascript.Append(@"document.write(""</a>"");");
            }
            else
            {
                AppendDecryptCallScript(javascript);
            }
            javascript.Append("\n-->\n");
            javascript.Append("</script>");
            return javascript.ToString();
        }

        private void AppendDecryptCallScript(StringBuilder javascript)
        {
            javascript.Append("document.write(decrypt(unescCtrlCh(");
            javascript.Append("\"");
            javascript.Append(encryptor.Encrypt(this.EmailAddress, Key));
            javascript.Append("\"),\"");
            javascript.Append(Key);
            javascript.Append("\"));");
        }

        protected override void CreateChildControls()
        {
            Controls.Add(_divResultContainer); 
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            #if NET_1_1
            this.Page.RegisterClientScriptBlock("decryptScript", encryptor.GetEncryptJavascriptFunctions());
            #endif
            #if NET_2_0 
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(),"decryptScript", encryptor.GetEncryptJavascriptFunctions());
            #endif

        }
        #endregion Inherited Methods

       

    }
}
